package com.guihuo.generator;

import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * 项目名称：java-web-leaning
 * 类名称：GeneratorServiceEntity
 * 类描述：GeneratorServiceEntity
 * 创建时间：2017/12/18 17:19
 *
 * @author zhangjun
 * @version v1.0
 */
public class GeneratorServiceEntity {

    private String packageName = "com.guihuo.domain";
    private String dbUrl = "jdbc:mysql://*************:3306/mogu_db";
    private String outputDir = "C:/Users/jf/Desktop/code";
    private String author = "guihuo";

    @Test
    public void generateCode() {
        String[] tables = "t_a t_b".split(" ");
        generateByTables(tables);
    }

    /**
     * 生成源码
     *
     * @param tableNames 表名
     */
    private void generateByTables(String... tableNames) {
        new AutoGenerator()
                .setGlobalConfig(new GlobalConfig()
                        .setActiveRecord(false)// 不需要ActiveRecord特性的请改为false
                        .setEnableCache(false)// XML 二级缓存
                        .setServiceName("%sService")
                        .setIdType(IdType.ID_WORKER_STR)
                        .setOutputDir(outputDir)
                        .setFileOverride(true)
                        .setAuthor(author)
                )
                .setDataSource(new DataSourceConfig()
                        .setDbType(DbType.MYSQL)
                        .setUrl(dbUrl)
                        .setUsername("")
                        .setPassword("")
                        .setDriverName("com.mysql.jdbc.Driver")
                )
                .setStrategy(new StrategyConfig()
                        .setTablePrefix("t_", "meta_")// 此处可以修改为您的表前缀
                        .setCapitalMode(true)
                        .setEntityLombokModel(true)
                        .setDbColumnUnderline(true)
                        .setNaming(NamingStrategy.underline_to_camel)
                        // 【实体】是否生成字段常量（默认 false）
                        // public static final String ID = "test_id";
                        .setEntityColumnConstant(true)
                        .setInclude(tableNames)//修改替换成你需要的表名，多个表名传数组)
                        .setSuperEntityClass("com.zhangtao.domain.entity.base.BaseEntity")// 自定义实体父类
                        .setSuperEntityColumns("CREATED_STAMP", "CREATED_TX_STAMP", "LAST_UPDATED_STAMP", "LAST_UPDATED_TX_STAMP", "STATE", "CREATED_BY", "MODIFIED_BY", "OWNER_BY", "SYS_IMG") // 自定义实体，公共字段
                )
                .setPackageInfo(new PackageConfig()
                        .setParent(packageName)
                        .setController("controller")
                        .setEntity("entity")
                ).execute();
    }

}
